package top.ffhome.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.hutool.core.map.MapUtil;
import top.ffhome.common.audit.AuditService;
import top.ffhome.common.service.impl.BaseServiceImpl;
import top.ffhome.system.constant.SysConstants;
import top.ffhome.system.entity.SysDictData;
import top.ffhome.system.entity.SysDictType;
import top.ffhome.system.mapper.SysDictTypeMapper;
import top.ffhome.system.service.SysDictDataService;
import top.ffhome.system.service.SysDictTypeService;

@Service
@AuditService(entity = SysDictType.class)
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictType, SysDictTypeMapper>
		implements SysDictTypeService {
	@Autowired
	private SysDictDataService dictDataService;

	@Override
	@CacheEvict(value = { "dictType_selectAll" }, allEntries = true)
	public void clearCache() {
	}

	@Override
	public boolean checkCategoryUnique(SysDictType dictType) {
		SysDictType dto = new SysDictType();
		dto.getParams().put("idNot", dictType.getId());
		dto.setCategory(dictType.getCategory());
		List<SysDictType> list = mapper.select(dto);
		if (list.size() > 0) {
			return false;
		}
		return true;
	}

	@Override
	@Cacheable(value = "dictType_selectAll", unless = "#result == null || #result.size() == 0")
	public Map<String, List<SysDictData>> selectAll() {
		Map<String, List<SysDictData>> map = MapUtil.newHashMap();
		SysDictType dto = new SysDictType();
		dto.setStatus(SysConstants.NORMAL);
		List<SysDictType> list = mapper.select(dto);
		for (SysDictType type : list) {
			map.put(type.getCategory(), dictDataService.selectByCategory(type.getCategory()));
		}
		return map;
	}
}
